package Dao;

import Model.Obj.*;
import Model.OrcUtils.JDBCUtils;

import javax.swing.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Vector;

public class UserDao {
    public static List<User> Login(String name, String pwd) {
        String sql = "select * from USERDATA where USERNAME=? and PASSWORD=?";
        List<User> list = JDBCUtils.JDBCselect(sql,User.class,name,pwd);
        return list;
    }


    public static int ChangePassword(String name, String pwd) {
        String sql = "update USERDATA set PASSWORD=? where USERNAME=?";
        int count = JDBCUtils.DataChange(sql,pwd,name);
        return count;
    }

    public static int ADDMEMBER(String cardNumber,int balance,int ds,String cp, String name, String user_id, String phone, String cardStatus) {
        String sql1 = "insert into MEMBER values(?,?,?)";
        int a = JDBCUtils.DataChange(sql1,name,phone,user_id);
        String sql2 = "insert into MEMBERCARD values(?,?,?,?)";
        int c = JDBCUtils.DataChange(sql2,cardNumber,balance,ds,cp);
        String sql3 = "insert into TAKINGCARD values(?,?,?,?)";
        int d = JDBCUtils.DataChange(sql3,cardStatus,cardNumber,name,user_id);
        if (c>= 0 && c >= 0 ){
            return 1;
        } else {
            return -1;
        }
    }

    public static List<MemberCard> selectMemberCardID(String id) {
        MemberCard memberCard = new MemberCard();
        List<MemberCard> list = new ArrayList<>();
        String sql3 = "select * from TAKINGCARD where CARDNO=?";
        List<TakingCard> list3 = JDBCUtils.JDBCselect(sql3,TakingCard.class,id);
        for (TakingCard takingCard : list3){
            if (takingCard.getCARDSTATUS().equals("禁用")){
                JOptionPane.showMessageDialog(null,"此卡被禁用");
                return null;
            }
            memberCard.setCARDNO(id);
            memberCard.setMEMBERNAME(takingCard.getMEMBERNAME());
            memberCard.setMEMBERID(takingCard.getMEMBERID());
        }
        String sql2 = "select * from MEMBERCARD where CARDNO=?";
        List<Card> list2 = JDBCUtils.JDBCselect(sql2,Card.class,id);
        for (Card card : list2){
            memberCard.setBALANCE(card.getBALANCE());
            memberCard.setDISCOUNT(card.getDISCOUNT());
            memberCard.setCARDSTATUS(card.getCARDSTATUS());
            memberCard.setCARDTYPE(card.getCARDTYPE());
        }
        String sql1 = "select * from MEMBER where MEMBERNAME=? and MEMBERID=?";
        List<Member> list1 = JDBCUtils.JDBCselect(sql1,Member.class,memberCard.getMEMBERNAME(),memberCard.getMEMBERID());
        for (Member member : list1){
            memberCard.setPHONE(member.getPHONE());
        }
        list.add(memberCard);
        return list;
    }

    public static List<MemberCard> selectMemberCardName(String name) {
        MemberCard memberCard = new MemberCard();
        List<MemberCard> list = new ArrayList<>();
        String sql3 = "select * from TAKINGCARD where MEMBERNAME=?";
        List<TakingCard> list3 = JDBCUtils.JDBCselect(sql3,TakingCard.class,name);
        for (TakingCard takingCard : list3){
            if (takingCard.getCARDSTATUS().equals("禁用")){
                JOptionPane.showMessageDialog(null,"此卡被禁用");
                return null;
            }
            memberCard.setCARDNO(takingCard.getCARDNO());
            memberCard.setMEMBERNAME(takingCard.getMEMBERNAME());
            memberCard.setMEMBERID(takingCard.getMEMBERID());
        }
        String sql2 = "select * from MEMBERCARD where CARDNO=?";
        List<Card> list2 = JDBCUtils.JDBCselect(sql2,Card.class,memberCard.getCARDNO());
        for (Card card : list2){
            memberCard.setBALANCE(card.getBALANCE());
            memberCard.setDISCOUNT(card.getDISCOUNT());
            memberCard.setCARDSTATUS(card.getCARDSTATUS());
            memberCard.setCARDTYPE(card.getCARDTYPE());
        }
        String sql1 = "select * from MEMBER where MEMBERNAME=? and MEMBERID=?";
        List<Member> list1 = JDBCUtils.JDBCselect(sql1,Member.class,memberCard.getMEMBERNAME(),memberCard.getMEMBERID());
        for (Member member : list1){
            memberCard.setPHONE(member.getPHONE());
        }
        list.add(memberCard);
        return list;
    }
    //12444

    public static List<SonSume> selectALLSonsume() {
        String sql = "select * from CONSUMPTIONHISTORY";
        List<SonSume> list = JDBCUtils.JDBCselect(sql,SonSume.class);
        return list;
    }


    public static List<SonSume> selectSonsume(String cardID) {
        String sql = "select * from CONSUMPTIONHISTORY where CARDNO=?";
        List<SonSume> list = JDBCUtils.JDBCselect(sql,SonSume.class,cardID);
        return list;
    }

    public static List<SonSume> selectSonsume(String cardID, String TIME1, String TIME2) {
        String sql = "select * from CONSUMPTIONHISTORY where CARDNO=?  and TIME between ? and ?";
        List<SonSume> list = JDBCUtils.JDBCselect(sql,SonSume.class,cardID,TIME1,TIME2);
        return list;
    }

    public static List<MemberCard> selectMemberCard(String cardid) {
        MemberCard memberCard = new MemberCard();
        String sql1 = "select * from MEMBERCARD where CARDNO=?";
        List<Card> list = JDBCUtils.JDBCselect(sql1,Card.class,cardid);
        for (Card card : list){
            memberCard.setBALANCE(card.getBALANCE());
            memberCard.setCARDTYPE(card.getCARDTYPE());
        }
        String sql2 = "select * from TAKINGCARD where CARDNO=?";
        List<TakingCard> list1 = JDBCUtils.JDBCselect(sql2,TakingCard.class,cardid);
        for (TakingCard takingCard : list1){
            memberCard.setCARDNO(takingCard.getCARDNO());
            memberCard.setCARDSTATUS(takingCard.getCARDSTATUS());
            if (takingCard.getCARDSTATUS().equals("退卡")){
                JOptionPane.showMessageDialog(null,"此卡已退卡");
                return null;
            }
        }
        List<MemberCard> list2 = new ArrayList<>();
        list2.add(memberCard);
        return list2;
    }

    public static int DeleteMemberCard(String cardno) {
        String sql1 = "update TAKINGCARD set CARDSTATUS=? where CARDNO=?";
        int count = JDBCUtils.DataChange(sql1,"退卡",cardno);
        return count;
    }

    public static int UpdateMemberCardStatus(String s, String cardno) {
        String sql1 = "update TAKINGCARD set CARDSTATUS=? where CARDNO=?";
        int count = JDBCUtils.DataChange(sql1,s,cardno);
        return count;
    }

    public static int UpdateMemberCardBalance(String balance, String cardno) {
        Integer b = Integer.valueOf(balance);
        String sql = "update MEMBERCARD set BALANCE=BALANCE+? where cardno=?";
        int count = JDBCUtils.DataChange(sql,b,cardno);
        return count;
    }

    public static int UpdateMemberCard(String cardno, String name, String phone, String memberid, Integer j, String cardtype, Integer i) {
        String sql2 = "select * from MEMBER where MEMBERID=?";
        int flag = -1;
        List<Member> list = JDBCUtils.JDBCselect(sql2,Member.class,memberid);
        if (list.size() == 0){
            String sql = "insert into MEMBER values(?,?,?)";
            flag = JDBCUtils.DataChange(sql,name,phone,memberid);
            String sql1 = "update MEMBERCARD set BALANCE=?,DISCOUNT=?,CARDTYPE=? where CARDNO=?";
            flag = JDBCUtils.DataChange(sql1,i,j,cardtype,cardno);
            String sql0 = "update TAKINGCARD set MEMBERNAME=?,MEMBERID=? where CARDNO=?";
            flag = JDBCUtils.DataChange(sql0,name,memberid,cardno);
        } else {
            for (Member member : list){
                if (member.getMEMBERNAME().equals(name)){
                    String sql3 = "update MEMBER set PHONE=? where MEMBERID=? and MEMBERNAME=?";
                    flag = JDBCUtils.DataChange(sql3,phone,memberid,name);
                    String sql1 = "update MEMBERCARD set BALANCE=?,DISCOUNT=?,CARDTYPE=? where CARDNO=?";
                    flag = JDBCUtils.DataChange(sql1,i,j,cardtype,cardno);
                    String sql = "update TAKINGCARD set MEMBERNAME=? where MEMBERID=?";
                    flag = JDBCUtils.DataChange(sql,name,memberid);
                } else {
                    String sql3 = "update MEMBER set MEMBERNAME=?,PHONE=? where MEMBERID=?";
                    flag = JDBCUtils.DataChange(sql3,name,phone,memberid);
                    String sql1 = "update MEMBERCARD set BALANCE=?,DISCOUNT=?,CARDTYPE=? where CARDNO=?";
                    flag = JDBCUtils.DataChange(sql1,i,j,cardtype,cardno);
                    String sql = "update TAKINGCARD set MEMBERNAME=? where MEMBERID=?";
                    flag = JDBCUtils.DataChange(sql,name,memberid);
                }
            }
        }
        return  flag;
    }
}
